package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.pojo.User;
import com.offcn.service.UserService;
import com.offcn.util.PageUtil;
import com.offcn.util.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    //创建UserService对象
    private UserService userService = new UserService();
    //创建全局对象
    private ResultVo resultVo = null;

    //注册功能
    public void register(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String pwd = request.getParameter("pwd");
        int register = userService.register(name, phone, pwd);
        if (register > 0) {
            resultVo = new ResultVo(200, "注册成功", null);
        } else {
            resultVo = new ResultVo(500, "注册失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //登录功能
    public void login(HttpServletRequest request,
                      HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String imageCode = request.getParameter("imageCode");
        HttpSession session = request.getSession();
        String verCode = (String) session.getAttribute("verCode");
        //验证码验证登录
        if (imageCode.equalsIgnoreCase(verCode)) {
            //验证通过，则验证账号密码
            User getUser = userService.login(username, password);
            if (getUser != null) {
                resultVo = new ResultVo(200, "登录成功!", getUser);
                session.setAttribute("getUser", getUser);
            }
        } else {
            resultVo = new ResultVo(101, "验证码输入错误！", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //获取登录人信息
    public void getUser(HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        User getUser = (User) session.getAttribute("getUser");
        resultVo = new ResultVo(200, "获取成功", getUser);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //退出登录功能
    public void loginOut(HttpServletRequest request,
                         HttpServletResponse response) throws Exception {
        //创建session对象
        HttpSession session = request.getSession();
        session.removeAttribute("getUser");
        resultVo = new ResultVo(200, "退出登录成功", null);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*增加*/
    public void addUser(HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        //创建bean对象
        User user = new User();
        //创建map集合
        Map<String, String[]> map = request.getParameterMap();

        //使用beanutil将map数据赋值给用户对象
        BeanUtils.populate(user, map);

        int i = userService.addUser(user);
        if (i > 0) {
            resultVo = new ResultVo(200, "增加成功", null);
        } else {
            resultVo = new ResultVo(101, "增加失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //分页查询
    public void queryAllUserByPage(HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        String pageSize = request.getParameter("pageSize");
        String currentPage = request.getParameter("currentPage");
        String search = request.getParameter("search");
        //调用后台查询方法
        PageUtil<User> pageUtil = userService.queryUserByPage(currentPage, pageSize, search);
        resultVo = new ResultVo(200, "查询成功！", pageUtil);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*批量删除用户*/
    public void deleteUserByIds(HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        //获取需要删除的用户id
        String ids = request.getParameter("uids");
        //调用后台删除的方法
        int i = userService.deleteUserByIds(ids);
        if (i > 0) {
            resultVo = new ResultVo(200, "删除成功", null);
        } else {
            resultVo = new ResultVo(101, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*保存修改后的用户信息*/
    public void updateUser(HttpServletRequest request,
                           HttpServletResponse response) throws Exception, IllegalAccessException {
        User user = new User();
        Map<String, String[]> map = request.getParameterMap();
        BeanUtils.populate(user, map);
        int i = userService.updateUser(user);
        if (i > 0) {
            resultVo = new ResultVo(200, "修改成功", null);
        } else {
            resultVo = new ResultVo(101, "修改失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }
}
